''' <summary>
''' Implements a PDF Form XObject
''' </summary>
''' <remarks>The term Form do not refer to an interactive form</remarks>
Friend Class clsPdfXObject
    Inherits clsPdfDictionnary
#Region "Private members"
    Private _stream As clsPdfStreamObject       'The stream of the XObject
#End Region
#Region "Constructors"
    ''' <summary>
    ''' Create a new Form XObject
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
        SubType = PDF_IMAGE
        'FormType = 1        'This is the only valid value for PDF standard version 1.7
        _stream = New clsPdfStreamObject

        BitsPerComponent = 8
        ColorSpace = ColorSpaces.DeviceRGB
    End Sub
    Public Sub New(stream As clsPdfStreamObject)
        _stream = stream
    End Sub
#End Region
#Region "Public Properties"
    ''' <summary>
    ''' Get the dictionnary for the XObject
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Dictionnary() As clsPdfDictionnary
        Get
            Dim key As String
            Dim obj As clsPdfBaseObject
            'The dictonnary for the XObject must include its stream's dictionnary
            Dictionnary = New clsPdfDictionnary(MyBase.Hashtable)
            For Each key In _stream.Dictionnary.Hashtable.Keys
                obj = _stream.Dictionnary.Item(key)
                Dictionnary.Add(key, obj, True)
            Next
        End Get
    End Property
    ''' <summary>
    ''' Get access to the stream object
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property StreamObject() As clsPdfStreamObject
        Get
            StreamObject = _stream
        End Get
    End Property
    Public Property BitsPerComponent() As Long
        Get
            BitsPerComponent = MyBase.Item(PDF_BITSPERCOMPONENT).GetLong
        End Get
        Set(ByVal value As Long)
            MyBase.Add(PDF_BITSPERCOMPONENT, CreateNumericObject(value), True)
        End Set
    End Property
    ''' <summary>
    ''' Get ot Set the XObject's color space
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ColorSpace() As ColorSpaces
        Get
            ColorSpace = _stringToColorSpaceType(MyBase.Item(PDF_COLORSPACE).GetString)
        End Get
        Set(ByVal value As ColorSpaces)
            Dim s As String = _colorSpaceTypeToString(value)
            MyBase.Add(PDF_COLORSPACE, CreateNameObject(s), True)
        End Set
    End Property
    ''' <summary>
    ''' Get or Set XObject's width
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Width() As Long
        Get
            Width = MyBase.Item(PDF_WIDTH).GetLong
        End Get
        Set(ByVal value As Long)
            MyBase.Add(PDF_WIDTH, CreateNumericObject(value), True)
        End Set
    End Property
    ''' <summary>
    ''' Get or Set XObject's height
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property Height() As Long
        Get
            Height = MyBase.Item(PDF_HEIGHT).GetLong
        End Get
        Set(ByVal value As Long)
            MyBase.Add(PDF_HEIGHT, CreateNumericObject(value), True)
        End Set
    End Property
    ''' <summary>
    ''' Get or Set the XObject's Bounding Box
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>This rectangle is used to clip the XObject</remarks>
    Public Property BoundingBox() As PdfRectangle
        Get
            BoundingBox = MyBase.Item(PDF_BBOX).GetRectangle
        End Get
        Set(ByVal value As PdfRectangle)
            MyBase.Add(PDF_BBOX, CreateRectangleObject(value), True)
        End Set
    End Property
    ''' <summary>
    ''' Get or Set the form type
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks>The only valid value defined in PDF version 1.7 is 1</remarks>
    Public Property FormType() As Long
        Get
            FormType = MyBase.Item(PDF_FORMTYPE).GetLong
        End Get
        Set(ByVal value As Long)
            MyBase.Add(PDF_FORMTYPE, CreateNumericObject(value), True)
        End Set
    End Property
    ''' <summary>
    ''' Get or Set the subtype
    ''' </summary>
    ''' <value>Annotation's type</value>
    ''' <returns>Annotation's type</returns>
    ''' <remarks></remarks>
    Public Property SubType() As String
        Get
            SubType = MyBase.Item(PDF_SUBTYPE).GetString
        End Get
        Set(ByVal value As String)
            MyBase.Add(PDF_SUBTYPE, CreateNameObject(value), True)
        End Set
    End Property
#End Region
#Region "Public Methods"
    ''' <summary>
    ''' Add a resource to the XObject
    ''' </summary>
    ''' <param name="key"></param>
    ''' <param name="pdfObject"></param>
    ''' <remarks></remarks>
    Public Sub AddResource(ByVal key As String, ByVal pdfObject As clsPdfBaseObject)
        MyBase.Add(key, pdfObject)
    End Sub
    ''' <summary>
    ''' Add a byte to the stream
    ''' </summary>
    ''' <param name="b"></param>
    ''' <remarks></remarks>
    Public Sub AddValue(ByVal b As Byte)
        _stream.AddValue(b)
    End Sub
#End Region
#Region "Private Methods"
    Private Function _colorSpaceTypeToString(ByVal type As ColorSpaces) As String
        Select Case type
            Case ColorSpaces.DeviceCMYK
                _colorSpaceTypeToString = PDF_DEVICECMYK
            Case ColorSpaces.DeviceGray
                _colorSpaceTypeToString = PDF_DEVICEGRAY
            Case ColorSpaces.DeviceRGB
                _colorSpaceTypeToString = PDF_DEVICERGB
            Case Else
                _colorSpaceTypeToString = ""
        End Select
    End Function
    Private Function _stringToColorSpaceType(ByVal type As String) As ColorSpaces
        Select Case type
            Case PDF_DEVICECMYK
                _stringToColorSpaceType = ColorSpaces.DeviceCMYK
            Case PDF_DEVICEGRAY
                _stringToColorSpaceType = ColorSpaces.DeviceGray
            Case PDF_DEVICERGB
                _stringToColorSpaceType = ColorSpaces.DeviceRGB
            Case Else
                _stringToColorSpaceType = ColorSpaces.DeviceRGB
        End Select
    End Function
#End Region
End Class
